package com.dcm360.report.alarm.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dcm360.report.alarm.domain.Alarm;
import com.dcm360.report.alarm.domain.DownloadData;
import com.dcm360.report.alarm.mapper.AlarmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/alarm")
public class AlarmController {

    @Autowired
    AlarmMapper alarmMapper;


    /**
     * 文件下载（失败了会返回一个有部分数据的Excel）
     */
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");


        List<Alarm> alarms = alarmMapper.selectAlarmList(new Alarm());

        alarms = alarms.stream().limit(10).collect(Collectors.toList());

        for(Alarm alarm : alarms){
            alarm.setUrl(new URL("http://192.168.100.244:8000/jk/room/5b88cf6863b911ec9600525400db17dc/192.168.100.243/02bae81ea4fe11ecb5b3525400db17dc/ins/0044ab4d4c7f46b8b9b915c3a3288045/1/A5--erweima1_0_6_1647940274545.jpg"));
        }

        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(response.getOutputStream()).build();
            WriteSheet ceshi = EasyExcel.writerSheet("ceshi").head(DownloadData.class).build();
            excelWriter.write(data(), ceshi);

            WriteSheet alarm = EasyExcel.writerSheet("alarm").head(Alarm.class).build();
            excelWriter.write(alarms, alarm);

        } finally {
            if(excelWriter != null){
                excelWriter.finish();
            }
        }

    }

    private List<DownloadData> data() {
        List<DownloadData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            DownloadData data = new DownloadData();
            data.setString("字符串" + 0);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }



    @GetMapping("/hello")
    public List hello(String hello){
        List<Alarm> alarms = alarmMapper.selectAlarmList(new Alarm());
        List<Alarm> alarmStream = alarms.stream().limit(20L).collect(Collectors.toList());

        return alarmStream;

    }

}
